package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.entity.Brand;
import cn.tedu.store.commons.pojo.product.entity.HomeBrand;
import cn.tedu.store.commons.pojo.product.vo.HomeBrandListItemVO;
import cn.tedu.store.commons.pojo.product.vo.HomeBrandStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.BrandMapper;
import cn.tedu.store.product.webapi.mapper.HomeBrandMapper;
import cn.tedu.store.product.service.IHomeBrandService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@DubboService
public class HomeBrandServiceImpl implements IHomeBrandService {
    @Autowired
    private HomeBrandMapper homeBrandMapper;
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void delete(Long id) {
        HomeBrandStandardVO result = homeBrandMapper.getStandardById(id);
        if(result == null){
            String message = "删除品牌失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = homeBrandMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        //删除品牌推荐后.品牌表中的推荐状态置0
        Brand brand = new Brand();
        brand.setId(result.getBrandId());
        brand.setIsRecommend(0);
        brandMapper.updateById(brand);
    }

    @Override
    public void setRecommend(Long id) {
        updateStatusById(id,1);
    }

    @Override
    public void setNotRecommend(Long id) {
        updateStatusById(id,0);
    }

    @Override
    public HomeBrandStandardVO getStandardById(Long id) {
        HomeBrandStandardVO result = homeBrandMapper.getStandardById(id);
        if(result == null){
            String message = "获取品牌详情失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }

    @Override
    public List<HomeBrandListItemVO> list() {
        return homeBrandMapper.list();
    }

    private void updateStatusById(Long id, Integer value){
        String[] tips = {"取消推荐","推荐"};
        HomeBrandStandardVO result = homeBrandMapper.getStandardById(id);
        if(result == null){
            String message = tips[value]+"品牌失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if(result.getRecommendStatus() == value){
            String message = "设置"+tips[value]+"品牌失败,该品牌已经是["+tips[value]+"]状态!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        HomeBrand homeBrand = new HomeBrand();
        homeBrand.setId(id);
        homeBrand.setRecommendStatus(value);
        int rows = homeBrandMapper.updateById(homeBrand);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        Brand brand = new Brand();
        brand.setId(result.getBrandId());
        brand.setIsRecommend(value);
        brandMapper.updateById(brand);
    }
}
